package com.gxa.modules.app.outpatient.medical.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.gxa.modules.app.outpatient.medical.dto.*;
import com.gxa.modules.app.outpatient.medical.service.MedicalService;
import com.gxa.modules.model.entity.hospital.MedicalRecord;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Api(tags = "体检服务")
@RestController
@RequestMapping("/app/medical")
public class MedicalController {

    @Autowired
    private MedicalService medicalService;

        @ApiOperation("查询类型套餐")
        @GetMapping("/querypackage")
        public R queryAll(){
            List<PackageTypeDto> packageTypes = this.medicalService.queryAll();
            return R.ok(packageTypes);
        }
        @ApiImplicitParam(paramType = "path", name = "id", value = "套餐类型id", dataType = "int")
        @ApiOperation("查询套餐")
        @PostMapping("/queryById/{id}")
        public R queryById(@PathVariable("id") Integer id){
            List<PackageDto> packageDtos = this.medicalService.queryById(id);
            return R.ok(packageDtos);
        }
        @ApiImplicitParam(paramType = "path", name = "id", value = "套餐id", dataType = "int")
        @ApiOperation("查询项目详情")
        @PostMapping("/queryByPackageId/{id}")
        public R queryByPackageId(@PathVariable("id") Integer id){
            List<ItemsDto> itemsDtos = this.medicalService.queryByPackageId(id);
            return R.ok(itemsDtos);
        }
        @ApiOperation("添加预约记录")
        @PostMapping("/add")
        public R queryByPackageId(@RequestBody MedicalRecord medicalRecord) {
            Double aDouble = this.medicalService.queryByPaintId(medicalRecord.getPatientId());
            if (medicalRecord.getSum() > aDouble) {
                return R.failed("请充值");
            } else {
                try {
                    Integer id = this.medicalService.add(medicalRecord);
                    return R.ok(id);
                } catch (Exception e) {
                    e.printStackTrace();
                    return R.failed("预约失败");
                }
            }
        }
        @ApiImplicitParam(paramType = "path", name = "id", value = "患者id", dataType = "int")
        @ApiOperation("查询金额")
        @PostMapping("/queryByPaintId/{id}")
        public R queryByPaintId(@PathVariable("id") Integer id){
            Double aDouble = this.medicalService.queryByPaintId(id);
            return R.ok(aDouble);
        }
        @ApiImplicitParam(paramType = "path", name = "id", value = "预约记录id", dataType = "int")
        @ApiOperation("查询预约信息")
        @PostMapping("/queryByRecord/{id}")
        public R queryByRecord(@PathVariable("id") Integer id){
            MedicalRecordDto medicalRecordDto = this.medicalService.queryByRecordId(id);
            return R.ok(medicalRecordDto);
        }

        @InitBinder
        protected void initBinder(WebDataBinder binder) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
        }




}
